﻿using B.S.Medical.Infrastructure;
using B.S.Medical.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.S.Medical.Infrastructure.Inpement
{
    /// <summary>
    /// 基础操作实现层
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回model实体</returns>
        public async Task<T> GetValue(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>返回所有数据</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> CreateAsync(T t)
        {
            db.Set<T>().Add(t);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回成功或失败</returns>
        public async Task<int> UpdateAsync(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public async Task<int> UpdateRangAsync(List<T> values)
        {
            db.Set<T>().UpdateRange(values);
            return await db.SaveChangesAsync();
        }
    }
}
